<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('提醒事项') }}</span>
    </div>
    <el-form v-if="false" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('问题简述')" prop="briefDescriptionOfTheProblem">
        <el-input
          v-model="queryParams.briefDescriptionOfTheProblem"
          :placeholder="$t('问题简述')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('问题等级')" prop="problemLevel">
        <el-input
          v-model="queryParams.problemLevel"
          :placeholder="$t('问题等级')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('是否已经发送过消息')" prop="whetherToSendAMessage">
        <el-input
          v-model="queryParams.whetherToSendAMessage"
          :placeholder="$t('是否已经发送过消息')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('是否处理')" prop="whetherToProcess">
        <el-input
          v-model="queryParams.whetherToProcess"
          :placeholder="$t('是否处理')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('是否为重大事项')" prop="isItAMajorMatter">
        <el-input
          v-model="queryParams.isItAMajorMatter"
          :placeholder="$t('是否为重大事项')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['yueyin:notes:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['yueyin:notes:import']"
          type="primary"
          icon="el-icon-upload"
          size="mini"
          @click="upload.open = true"
        >{{ $t('导入') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sysYueyinDealNotesList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('问题简述')" align="center" prop="briefDescriptionOfTheProblem" />
      <el-table-column :label="$t('具体描述')" align="center" prop="specificDescription" />
      <el-table-column :label="$t('时间限制')" align="center" prop="timeLimit" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.timeLimit) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('负责人')" align="center" prop="principalName" />
      <el-table-column :label="$t('目前现状')" align="center" prop="currentStatus" />
      <el-table-column :label="$t('问题等级')" align="center" prop="problemLevel" :formatter="problemLevelFormat" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['yueyin:notes:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['yueyin:notes:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改约印医疗-项目管理-提醒事项对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <sysYueyinDealNotesForm ref="sysYueyinDealNotesForm" />
    </el-dialog>

    <!-- 导入窗口 -->
    <el-dialog :title="$t('导入')" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false">
      <div>
        <el-upload
          ref="upload"
          v-loading="upload.isUploading"
          :limit="1"
          accept=".xlsx, .xls"
          :headers="upload.headers"
          :action="upload.url"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :on-exceed="handleFileExceed"
          :on-change="handleFileChange"
          :auto-upload="false"
          drag
        >
          <i class="el-icon-upload" />
          <div class="el-upload__text">
            {{ $t('将文件拖到此处，或') }}
            <em>{{ $t('点击上传') }}</em>
          </div>
          <div slot="tip" class="el-upload__tip" style="color:red">
            {{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}
            <el-link type="primary" style="font-size:12px" @click="fetchTemplate">
              {{ $t('下载模板') }}
            </el-link>
          </div>
        </el-upload>

        <div slot="footer" class="dialog-footer" style="text-align: right;">
          <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysYueyinDealNotes, delSysYueyinDealNotes, exportSysYueyinDealNotes } from '@/api/sass/yueyin/deal/notes/sysYueyinDealNotes'
import sysYueyinDealNotesForm from './sysYueyinDealNotesForm.vue'
// import { getUser } from '@/api/system/user'
import { getToken } from '@/utils/auth'

export default {
  components: {
    sysYueyinDealNotesForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 约印医疗-项目管理-提醒事项表格数据
      sysYueyinDealNotesList: [],
      // 弹出层标题
      title: '提醒事项',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        dealId: this.$route.query.id,
        briefDescriptionOfTheProblem: undefined,
        specificDescription: undefined,
        timeLimit: undefined,
        principalId: undefined,
        currentStatus: undefined,
        problemLevel: undefined,
        whetherToSendAMessage: undefined,
        whetherToProcess: undefined,
        isItAMajorMatter: undefined,
        processingDate: undefined,
        howToHandle: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      problemLevelArr: [],
      /* 导入参数 */
      upload: {
        // 是否显示弹出层（项目导入）
        open: false,
        // 弹出层标题（项目导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken(), dealType: this.$route.query.dealType },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/notes/sysYueyinDealNotes/importData/' + this.$route.query.id
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('deal_notes_problemLevel').then(response => {
      this.problemLevelArr = response.data
    })
  },
  methods: {
    problemLevelFormat(row) {
      return this.selectDictLabel(this.problemLevelArr, row.problemLevel)
    },
    /** 查询约印医疗-项目管理-提醒事项列表 */
    getList() {
      this.loading = true
      listSysYueyinDealNotes(this.queryParams).then(response => {
        this.sysYueyinDealNotesList = response.rows
        /* this.sysYueyinDealNotesList.forEach(item => {
          getUser(item.principalId).then(result => {
            this.$set(item,'principalName',result.data.nickName)
          })
        })*/

        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,

        dealId: undefined,

        briefDescriptionOfTheProblem: undefined,

        specificDescription: undefined,

        timeLimit: undefined,

        principalId: undefined,

        currentStatus: '0',

        problemLevel: undefined,

        whetherToSendAMessage: undefined,

        whetherToProcess: undefined,

        isItAMajorMatter: undefined,

        processingDate: undefined,

        howToHandle: undefined,

        actId: undefined,

        actStatus: '0',

        actTitle: undefined,

        remark: undefined,

        createBy: undefined,

        updateBy: undefined,

        createTime: undefined,

        updateTime: undefined,

        delFlag: undefined

      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.sysYueyinDealNotesForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.sysYueyinDealNotesForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delSysYueyinDealNotes(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportSysYueyinDealNotes(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      if (response.msg === 'success') {
        this.msgSuccess(this.$t('导入成功'))
        this.getList()
      } else if (response.code === 500) {
        this.msgError(response.msg)
      } else {
        this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      }
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 文件达到上限处理
    handleFileExceed() {
      this.msgError(this.$t('可上传文件数量已达到上限'))
    },
    // 文件上传变更事件
    handleFileChange(file, fileList) {
      console.log(file.name)
      if (file.name.indexOf('xls') < 0 && file.name.indexOf('xlsx') < 0) {
        this.msgError(this.$t('仅允许导入“xls”或“xlsx”格式文件！'))
        fileList.splice(0, 1)
      }
    },
    fetchTemplate() {
      const currentHref = window.location.href
      // 根据项目类型下载不同的模板
      const templateRoute = '/template/yueyin/reminderImportTemplate.xls'
      if (templateRoute !== '') {
        window.location.href = currentHref.substring(0, currentHref.indexOf(this.$route.path)) + templateRoute
      }
    }
  }
}
</script>
